global input "Q:\dc1prhcmsas01\PU2\data - sas"
global temp "Q:\dc1prhcmsas01\PU2\temp_stata_wr"
global log "Q:\dc1prhcmsas01\PU2\Log - Stata WR"
global output "Q:\dc1prhcmsas01\PU2\data_stata_wr"
global graphs "Q:\dc1prhcmsas01\PU2\graphs_stata_wr"
global tables "Q:\dc1prhcmsas01\PU2\tables_stata_wr"

cd "Q:\dc1prhcmsas01\PU2\Code - Stata WR"

capture log close
log using "$log/6_fig_bunch_mass", replace

set scheme plotplain
graph set window fontface "Times New Roman"


*Fig 3:
	*Calculate share in treated, collapse by date, difference relative to April 2016
	use "$output\stayers_distribution_treat.dta", clear
	keep if salaried==1
	replace bin=floor((bin-913)/40)*40+913
	collapse (sum) n, by(yr_month client_id salaried0 bin)
	bysort yr_month client_id: egen emp=sum(n)
	keep if bin==913	
	gen share=n/emp
	gen treat=1
	gen time=mofd(yr_month)-mofd(mdy(4,01,2016))
	tempfile treat
	save `treat'
	
	*Calculate share in control, collapse by date, difference relative to April 2014
	use "$output\stayers_distribution_control.dta", clear
	keep if salaried==1
	replace bin=floor((bin-913)/40)*40+913
	collapse (sum) n, by(yr_month client_id salaried0 bin)
	bysort yr_month client_id: egen emp=sum(n)
	keep if bin==913 
	gen share=n/emp
	gen treat=0
	gen time=mofd(yr_month)-mofd(mdy(4,01,2014))
	append using `treat'
	
	*Calculate diff-in-diff by month 
	forvalues i=1/37 {
		gen d`i'=treat*(time==`i'-12)
	}
	drop d12
	gen event_time=.
	gen beta=.
	gen lb=.
	gen ub=.
	
	reghdfe share d*, absorb(treat time) nocon vce(cluster client_id)
	forvalues i=1/37 {
		replace event_time=`i'-12 if _n==`i'
		
		if `i'!=12 {
		replace beta=_b[d`i'] if _n==`i'
		replace ub = _b[d`i']+1.96*_se[d`i'] if _n==`i'
		replace lb = _b[d`i']-1.96*_se[d`i'] if _n==`i'
		}
		if `i'==12 {
		replace beta=0 if _n==`i'
		replace ub =0 if _n==`i'
		replace lb =0 if _n==`i'
		}
	}
	keep if beta!=.
	keep event_time beta ub lb	
	
	*Save estimates
	save "$output/fig3_estimates.dta", replace

	label define time_lab -12 "Apr 2015" -11 "May 2015" -10 "Jun 2015" ///
		-9 "Jul 2015" -8 "Aug 2015" -7 "Sep 2015" -6 "Oct 2015" -5 "Nov 2015" -4 "Dec 2015" ///
		-3 "Jan 2016" -2 "Feb 2016" -1 "Mar 2016" 0 "Apr 2016" 1 "May 2016" 2 "Jun 2016" ///
		3 "Jul 2016" 4 "Aug 2016" 5 "Sep 2016" 6 "Oct 2016" 7 "Nov 2016" 8 "Dec 2016" ///
		9 "Jan 2017" 10 "Feb 2017" 11 "Mar 2017" 12 "Apr 2017" 13 "May 2017" 14 "Jun 2017" ///
		15 "Jul 2017" 16 "Aug 2017" 17 "Sep 2017" 18 "Oct 2017" 19 "Nov 2017" 20 "Dec 2017" ///
		21 "Jan 2018" 22 "Feb 2018" 23 "Mar 2018" 24 "Apr 2018" 25 "May 2018", replace
	label values event_time time_lab 
	
	*Plot 
	graph twoway (connect beta event_time, lc(black) mcolor(black)) (rcap ub lb event_time, color(black)), ///
		xline(1 8, lcolor(black)) xline(15, lcolor(red)) xtitle("Month") xlabel(-12(3)25, valuelabel angle(45)) ///
		yline(0) ytitle("Effect on Share of Salaried Workers" "Earning $913/Week") ylabel(-0.0025(0.0025)0.0125)  legend(off)
	graph export "$graphs/fig3_bunch_mass.eps", replace
	graph export "$graphs/fig3_bunch_mass.jpg", replace
log close 

